// pages/operation/classRecord/classMakeUpTimes/index.js
import {
  ClassModel
} from "../../../../api/api-work/classModel.js";
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
const classModel = new ClassModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    total: 0,
    firstTempDatas: [],
    firstLoadStatus: "",
    isFirstTriggered: false,
    classId: "",
    className: "",
    studentId: "",
    orderNo: "",
    studentName: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const param = JSON.parse(options.param);
    console.log('params--------------', param);
    console.log('sssss', param);
    this.setData({
      classId: param.classId,
      studentId: param.studentId,
      orderNo: param.orderNo,
      studentName: param.studentName,
      className: param.className
    })
    await this.getOrdeList()
  },
  //算周几
  getDayOfWeek(dateString) {
    const date = new Date(dateString);
    const days = ['日', '一', '二', '三', '四', '五', '六'];
    return days[date.getDay()];
  },
  // 获取点名记录
  async getOrdeList() {
    classModel.getOrdeListApi({
      pageNum: this.data.pageNum,
      pageSize: 15,
      classId: this.data.classId,
      studentIdMissed: this.data.studentId
    }).then(res => {
      // if (res.rows.length > 0) {
      //   res.rows.forEach(item => {
      //     item["callTimeNew"] = item.callTime.split(" ")[1]
      //   })
      // }
      let rows = this.data.firstTempDatas;
      rows.push(...res.rows);
      this.setData({
        firstTempDatas: rows,
        firstTotal: res.total,
      })
      // 数据格式整合
      let obj = {}
      this.data.firstTempDatas.forEach(item => {
        const time = item.classDate;
        if (obj.hasOwnProperty(time)) {
          obj[time].push(item)
        } else {
          obj[time] = [item]
        }
      })
      this.setData({
        firstDataObj: obj
      })

      if (this.data.firstTotal == 0) {
        this.setData({
          // 无数据
          firstLoadStatus: ""
        })
      } else if (this.data.firstTempDatas.length < this.data.firstTotal) {
        // 
        this.setData({
          firstLoadStatus: "loadmore"
        })
      } else {
        this.setData({
          firstLoadStatus: "nomore"
        })
      }
    })
  },
  getMoreCalls() {
    if (this.data.firstLoadStatus === "nomore") {
      // 没有更多了
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      firstLoadStatus: "loading",
    })
    this.getOrdeList();
  },
  refreshMoreCalls() {
    this.setData({
      isFirstTriggered: false,
      pageNum: 1,
      firstDataObj: {},
      firstTempDatas: []
    })
    this.getOrdeList()
  },
  toAddClass(item) {
    const data = item.currentTarget.dataset.query;
    const params = {
      classDates: data.classDate,
      cofId: data.cofId,
      ids: [{
        id: this.data.studentId,
        courseId: data.courseId
      }],
      orderNo: data.orderNo,
      temp: 2,
      tempOrderNo: this.data.orderNo
    }
    // console.log(params);
    console.log(data);
    Dialog.confirm({
        title: '插班补课提醒',
        message: `学员${this.data.studentName}将被安排进入【${this.data.className}】${data.classDate}(周${this.getDayOfWeek(data.classDate)})${data.classStartTime}-${data.classEndTime}进行补课。补课后将扣除课程"${data.courseName}"${data.classHour}课时，是否确认添加?`,
      })
      .then(() => {
        classModel.addStudentApi(params).then(res => {
          wx.navigateBack({
            delta: 3,
          })
          Toast('插班成功');
        })
      }).catch(() => {
        // on cancel
      });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // await this.getOrdeList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})